package com.admin.education.dataTransferObject.circles.Mapper;

import com.admin.education.dataTransferObject.circles.AdmirersTransfer;
import com.admin.education.dataTransferObject.circles.CirclesTransfer;
import com.admin.education.dataTransferObject.circles.CommentsTransfer;
import com.admin.education.models.*;
import com.admin.education.repositories.UserInformationRepository;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper
public abstract class AdmirersMapper {
    private QUserInformation qUserInformation;
    public AdmirersMapper(){
        qUserInformation= QUserInformation.userInformation;
    }
    @Autowired
    private UserInformationRepository userInformationRepository ;

    @Mappings({})
    public abstract AdmirersTransfer mapFrom(Admirers admirers);
    @Mappings({})
    public abstract List<AdmirersTransfer> mapFrom(List<Admirers> admirers);
    @AfterMapping
    public void afterMapping(Admirers admirers, @MappingTarget AdmirersTransfer admirersTransfer){
        UserInformation userInformation= this.userInformationRepository.selectFrom(qUserInformation).where(qUserInformation.id.eq(admirers.getUserInformationId())).fetchFirst();

        if (null != userInformation) {
            admirersTransfer.setName(userInformation.getName());
            if (null != userInformation.getClasses()) {
                admirersTransfer.setClassName(userInformation.getClasses().getName());
            }
            if (null != userInformation.getSchools()) {
                admirersTransfer.setSchoolName(userInformation.getSchools().getName());
            }
            if (null != userInformation.getClasses().getGrades()) {
                admirersTransfer.setGradeName(userInformation.getClasses().getGrades().getName());
            }
        }
    }
}
